//
//  MemberAlertView.swift
//  readtool
//
//  Created by Wan Jiading on 2017/3/30.
//  Copyright © 2017年 DAD. All rights reserved.
//

import UIKit

@objc protocol MemberAlertViewDelegate: NSObjectProtocol {
	@objc optional func memberAlertView(_ memberAlertView: MemberAlertView, clickedButtonAtIndex buttonIndex: Int)
}

class MemberAlertView: UIView {
	
	var delegate: MemberAlertViewDelegate?
	
	private var backgroundView = UIView()
	private var imageView = UIImageView()
	private var titleLabel = UILabel()
	private var contentLabel = UILabel()
	private var btnView = UIView()
	private var cancelButton = UIButton()
	
	private var maskButton = UIButton()
	
    private let ScreenSize = UIScreen.main.bounds.size
	private let ScreenWidth = (MainScreen.bounds.size.width)
	private let ScreenHeight = (MainScreen.bounds.size.height)
	private let ContentButtonVerticalMargin:CGFloat = 20        // content中按钮 距离顶部 和 底部的 距离
	private let ContentButtonHeight:CGFloat = 76
	
	convenience init(_ title: String?, _ imageUrlString: String, _ buttonTitles: [String], delegate: MemberAlertViewDelegate?) {
		self.init()
		
		self.delegate = delegate
		
		// 背景按钮
		maskButton.backgroundColor = UIColor.rgb("000000", alpha: 0.5)
		maskButton.frame = MainScreen.bounds
		maskButton.addSubview(backgroundView)
		maskButton.addTarget(self, action: #selector(dismiss), for: UIControlEvents.touchUpInside)
		self.addSubview(maskButton)
		
		// 主题内容背景view
		let imageWidth: CGFloat = 293
		let imageHeight: CGFloat = 341
		
		backgroundView.backgroundColor = UIColor.white
		backgroundView.frame = CGRect(x: 0, y: 0, width: imageWidth, height: imageHeight)
		backgroundView.center = CGPoint(x: ScreenSize.width * 0.5, y: ScreenSize.height * 0.5)
		backgroundView.layer.cornerRadius = 10
		
		// imageView
		imageView.frame = CGRect(x: 0, y: 0, width: imageWidth, height: imageHeight)
//		imageView.sd_setImage(with: URL(string: imageUrlString))
		imageView.isUserInteractionEnabled = true
		backgroundView.addSubview(imageView)
	
		// 按钮
		let btnMargin: CGFloat = 10
		let btnBottom: CGFloat = 20
		let btnW: CGFloat = 132
		let btnH: CGFloat = 40
		let btnY: CGFloat = imageView.height - btnH - btnBottom
		
		var hightColor = UIColor.rgb("9bc34e")
		var normalColor = UIColor.white
		var tempColor = UIColor()
		for i in 0..<buttonTitles.count {
			if (i == buttonTitles.count - 1) {
				tempColor = hightColor
				hightColor = normalColor
				normalColor = tempColor
			}
			
			let btn = UIButton(title: buttonTitles[i], titleColor: hightColor, backgroundColor: normalColor, fontSize: 14, target: self, action: #selector(btnClick(_:)))
			btn.tag = 1000 + i
			btn.layer.cornerRadius = 3
			btn.layer.borderWidth = GlobalConst.DAD1PX
			btn.layer.borderColor = hightColor.cgColor
			let btnX: CGFloat = btnMargin * CGFloat(i + 1) + CGFloat(i) * btnW
			btn.frame = CGRect(x: btnX, y: btnY, width: btnW, height: btnH)
			imageView.addSubview(btn)
		}
		
		self.frame = maskButton.bounds
	}
	
	
	func btnClick(_ btn: UIButton) {
		DebugLog(btn.tag - 1000)
		if (self.delegate != nil && self.delegate!.responds(to: #selector(MemberAlertViewDelegate.memberAlertView(_:clickedButtonAtIndex:)))) {
			self.delegate!.memberAlertView!(self, clickedButtonAtIndex: btn.tag - 1000)
		}
	}
	
	
	public func show() {
		let animation = CAKeyframeAnimation(keyPath: "transform")
		animation.duration = 0.25
		
		let values = [
			NSValue.init(caTransform3D: CATransform3DMakeScale(0.8, 0.8, 1.0)),
			NSValue.init(caTransform3D: CATransform3DMakeScale(1.01, 1.01, 1.0)),
			NSValue.init(caTransform3D: CATransform3DMakeScale(0.99, 0.99, 1.0)),
			NSValue.init(caTransform3D: CATransform3DMakeScale(1.0, 1.0, 1.0)),
			]
		animation.values = values
		backgroundView.layer.add(animation, forKey: nil)
		
		maskButton.backgroundColor = UIColor.rgb("000000", alpha: 0.5)
		UIApplication.shared.keyWindow?.addSubview(self)
	}
	
	
	func dismiss() {
		let animation = CAKeyframeAnimation(keyPath: "transform")
		animation.duration = 0.25
		let values = [
			NSValue.init(caTransform3D: CATransform3DMakeScale(1.0, 1.0, 1.0)),
			NSValue.init(caTransform3D: CATransform3DMakeScale(0.8, 0.8, 1.0)),
			NSValue.init(caTransform3D: CATransform3DMakeScale(0.1, 0.1, 1.0)),
			]
		animation.values = values
		backgroundView.layer.add(animation, forKey: nil)
		
		UIView.animate(withDuration: 0.25, delay: 0, options: UIViewAnimationOptions.curveEaseOut, animations: { [unowned self] in
            self.maskButton.alpha = 0
        }, completion:{ [unowned self] (finished) in
            self.removeFromSuperview()
		})
	}

}
